<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/1/13
 * Time: 13:39
 */

namespace app\admin\controller;

use think\Db;
class Yj extends Base
{
    /**
     * 积分明细列表
     */
    public function index(){
        if(request()->isAjax ()){
            $inte_num = trim(input('inte_num'), ' '); // 流水号
            $user_real_name = trim(input('user_real_name'), ' '); // 用户名
            $user_phone = trim(input('user_phone'), ' '); // 手机号
            $start_time = input('start_time');
            $end_time = input('end_time');
            $inte_type = input('inte_type'); // 1 收入  2 支出
            
            $where['inte_type'] = ['between', [3, 7]];
            if(isset($inte_num) && $inte_num != ''){
                $where['inte_num'] = ['like', '%' . $inte_num . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['inte_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['inte_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['inte_time'] = ['<=', $end_time];
            }
            if(isset($inte_type) && $inte_type != ''){
                if($inte_type == 1){
                    $where['inte_type'] = ['between', [3, 4]];
                }else{
                    $where['inte_type'] = ['between', [5, 7]];
                }
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_integral')
                ->join('up_u_user', 'inte_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $count2 = Db::table('up_integral')
                ->join('up_u_user', 'inte_user_id = user_id', 'LEFT')
                ->where($where)
                ->column('inte_id');
            $lists = Db::table('up_integral')
                ->join('up_u_user', 'inte_user_id = user_id', 'LEFT')
                ->field('inte_id, inte_type, inte_num, inte_des, inte_number, inte_front, inte_after, 
                inte_time, user_real_name, user_phone')
                ->where($where)
                ->order('inte_time desc, inte_sort asc')
                ->page($Nowpage,$limits)
                ->select();
            $str = '';
            if($count2){
                $str = implode(',', $count2);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'count2' => $str]);
        }
        return $this->fetch();
    }
    
    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $inte_ids = input('ids');
        if($inte_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
        
        $lists = Db::table('up_integral')
            ->join('up_u_user', 'inte_user_id = user_id', 'LEFT')
            ->field('inte_id, inte_type, inte_num, inte_des, inte_number, inte_front, inte_after, 
                inte_time, user_real_name, user_phone')
            ->where('inte_id', 'IN', $inte_ids)
            ->order('inte_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 类型
            if($v['inte_type'] == 3 || $v['inte_type'] == 4){
                $lists[$k]['inte_type'] = '收入';
            }elseif($v['inte_type'] == 5 || $v['inte_type'] == 6 || $v['inte_type'] == 7){
                $lists[$k]['inte_type'] = '支出';
            }
//            else{
//                $lists[$k]['inte_type'] = '佣金';
//            }
            // 流水号
            $lists[$k]['inte_num'] = "\t" . $v['inte_num'] . "\t";
            // 积分值
            $lists[$k]['inte_number'] = "\t" . $v['inte_number'] . "\t";
            // 变动前积分
            $lists[$k]['inte_front'] = "\t" . $v['inte_front'] . "\t";
            // 变动后积分
            $lists[$k]['inte_after'] = "\t" . $v['inte_after'] . "\t";
            // 手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
        }
        $cellname = [
            ['user_real_name', '用户', 10, 'LEFT'],
            ['user_phone', '用户手机号', 20, 'LEFT'],
            ['inte_type', '类型', 10, 'LEFT'],
            ['inte_des', '描述', 20, 'LEFT'],
            ['inte_num', '流水号', 25, 'LEFT'],
            ['inte_front', '变动前佣金', 20, 'LEFT'],
            ['inte_number', '佣金值', 15, 'LEFT'],
            ['inte_after', '变动后佣金', 20, 'LEFT'],
            ['inte_time', '记录时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('佣金明细', 'Yjin', $cellname, $lists);
        return json($res);
    }
}